<?php


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
loadcache('plugin');
if(!defined('Adz_class')){include('Adz.class.php');}
include('acan_weixin.fun.php');
$referer = dreferer();

if($_GET['op']!='disconnect'){
	if($_SESSION['weixin']['info']){
		$info = $_SESSION['weixin']['info'];
	}else{
		$rs = acan_py('userinfo');
		if($rs->status==1){
			$_SESSION['weixin']['info'] = $info = $rs->data;
		}else{
			$wxlogin = $_G['cache']['plugin']['acan_weixin']['login'];
			unset($_SESSION['weixin']);
			showmessage(lang('plugin/acan_weixin', 'weixinautherror'), $wxlogin, array(), array('timeout' => '3', 'alert' => 'right'));
		}
	}
	$weixin_id=$info->id;
	$weixin_info = array('weixin_id' => $weixin_id, 'profile' => json_encode($_SESSION['weixin']['info']));
}
if($_GET['op']=='bind'){
	include template('common/header_ajax');
	$save = $_POST['save'];
	if($_GET['style']==1){//创建新
		if($save['password'] != $_POST['password'] || $save['password'] == ''){
			echo '17';
		} else {
			if(acan_fetch_uid($save['username'])) {
				echo '11'; //username_duplicate
			} else {
				loaducenter();
				$uid = uc_user_register($save['username'], $save['password'], $save['email']);
				if($uid <= 0) {
					$arr = array(-1=>'12',-2=>'13',-3=>'11',-4=>'14',-5=>'15',-6=>'16');
					echo $arr[$uid];
				} else {
					$sql = "SELECT * FROM ".DB::table('common_usergroup').' WHERE groupid=\''.$_G['cache']['plugin']['acan_weixin']['group'].'\'';
					$group = DB::fetch_first($sql);
					$newadminid = in_array($group['radminid'], array(1, 2, 3)) ? $group['radminid'] : ($group['type'] == 'special' ? -1 : 0);
					loadcache('fields_register');
					$init_arr = explode(',', $_G['setting']['initcredits']);
					$save['uid'] = $uid;
					$save['groupid'] = $_G['cache']['plugin']['acan_weixin']['group'];
					$save['adminid'] = $newadminid;
					addmember($save, array('credits' => $init_arr));
					if($_G['cache']['plugin']['acan_weixin']['credit']) {
						$credit_style = $_G['cache']['plugin']['acan_weixin']['credit'];
						$sql = 'SELECT extcredits'.$credit_style.' FROM '.DB::table('common_member_count')." WHERE uid='$uid'";
						$ucredit = DB::fetch_first($sql);
						$data = array('extcredits'.$credit_style => $ucredit['extcredits'.$credit_style] + $_G['cache']['plugin']['acan_weixin']['credit_quan']);
						DB::update("common_member_count", $data, "uid='$uid'");
					}
					$weixin_info['uid'] = $uid;
					$insertid = addbindinfo($weixin_info); //add to bind table
					if($insertid) {
						connect_login($uid);
						manageaftlogin($uid);
						echo '10'; //ok
					} else {
						echo $insertid; //fatalerror
					}
				}
			}
		}
	}else{//绑定已有帐号
		if(function_exists('fetch_uid_by_username')) {
			$uid = C::t('common_member')->fetch_uid_by_username($save['username']);
		} else {
			$uid = acan_fetch_uid($save['username']);
		}
		$sql = 'SELECT uid FROM '.DB::table('plugin_weixin')." WHERE weixin_id='".$weixin_id."' or uid='$uid'";
		$rs = DB::fetch_first($sql);
		if(!empty($rs)){
			echo '3';//binded yet
		} else {
			//not binded, bind existing forum user
			$sql = 'SELECT salt,uid,password FROM '.DB::table('ucenter_members')." WHERE username='".$save['username']."'";
			$rs = DB::fetch_first($sql);
			$salt = $rs['salt'];
			if(!empty($rs)) { //salt found
				if(md5(md5($save['password']).$salt) == $rs['password']) {
					//true password,begin bind
					$weixin_info['uid'] = $rs['uid'];
					$insertid = addbindinfo($weixin_info); //bind info insert database
					if($insertid) {
						$niuc_uinfo = array('uid' => $rs['uid']);
						connect_login($uid); //login
						manageaftlogin($uid); //login extra
						echo '0';  // OK
					} else {
						echo '4'; //fatal error
					}
				} else {
					echo '2'; //wrong password
				}
			} else {
				echo '1'; //salt not found
			}
		}
	}
	include template('common/footer_ajax');
}elseif($_GET['op'] == 'disconnect') {
	if($_G['uid']) {
		$binded = Adz::fetch_con('plugin_weixin',array('uid'=>$_G['uid']));
		if(!$binded) {
			showmessage(lang('plugin/acan_weixin', 'notbinded'));
		} else {
			DB::delete('plugin_weixin', "uid='".$_G['uid']."'");
			showmessage(lang('plugin/acan_weixin', 'disbindsuccess'), $_SERVER['HTTP_REFERER'], array(), array('timeout' => '1', 'alert' => 'right'));
		}
	} else {
		showmessage(lang('plugin/acan_weixin', 'needlogin'), '', array(), array('login' => true));
	}
}else{
	if($weixin_id){
		$wxinfo = Adz::fetch_con('plugin_weixin',array('weixin_id'=>$weixin_id));
		if($wxinfo){
			if($_G['uid'] && $_G['uid'] != $wxinfo['uid']) {
				showmessage(lang('plugin/acan_weixin', 'chnweixinuserbeforebind'));
			} else {
				connect_login($wxinfo['uid']);
				manageaftlogin($wxinfo['uid']);
				loadcache('usergroups');
				$usergroups = $_G['cache']['usergroups'][$_G['groupid']]['grouptitle'];
				$param = array('username' => $_G['member']['username'], 'usergroup' => $_G['group']['grouptitle']);
				showmessage('login_succeed', './', $param, array('extrajs' => $ucsynlogin, 'showdialog' => 1, 'locationtime' => true));
			}
		}
	}
	if($_G['uid']){//bind loginned forum user?
		$examuid = Adz::fetch_con('plugin_weixin',array('uid'=>$_G['uid']));
		if(empty($examuid))	{	//loginned forum user not binded
			$weixin_info['uid'] = $_G['uid'];
			$rtn = addbindinfo($weixin_info); //bind info insert database
			if($rtn) {
				showmessage(lang('plugin/acan_weixin', 'bindsuccess').lang('plugin/acan_weixin', 'weixinuser').$info->username, '', array(), array('timeout' => '1', 'alert' => 'right'));
			} else {
				showmessage(lang('plugin/acan_weixin', 'bindfailure').lang('plugin/acan_weixin', 'fatalerror'));
			}
		} else {
			//loginned forum user has binned baidu,and loginned baiduuser not binned
			showmessage(lang('plugin/acan_weixin', 'logoutbeforeweixinlogin').'<br />'.lang('plugin/acan_weixin', 'weixinuser').$info->username);
		}
	}
}
include_once template("acan_weixin:callback");


?>